package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bp.hearthealth.pulse.lnapps.R
import com.bp.hearthealth.pulse.lnapps.bp_extension.UIHelperBp
import com.bp.hearthealth.pulse.lnapps.databinding.LanguageItemLayoutBgBinding
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_bean.LanguageItemBean
import com.bp.hearthealth.pulse.lnapps.bp_utils.DoubleClickListener
import com.bp.hearthealth.pulse.lnapps.bp_utils.throttleClick

class BpLanguageTypeAdapter :
    RecyclerView.Adapter<BpLanguageTypeAdapter.ItemVH>() {

    private var list = mutableListOf<LanguageItemBean>()
    private var lastPosition = 0

    fun setData(list: MutableList<LanguageItemBean>) {
        this.list = list
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemVH {
        return ItemVH(
            LanguageItemLayoutBgBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    inner class ItemVH(val binding: LanguageItemLayoutBgBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindData(item: LanguageItemBean, position: Int) {
            binding.bean = item
            if (position == 0) {
                item.languageSelect = !item.languageSelect
                binding.tvLanguageType.text = UIHelperBp.getString(R.string._default_str)
            } else {
                binding.tvLanguageType.text = item.languageType
            }
            itemView.throttleClick {
                if (list[lastPosition].languageSelect) {
                    list[lastPosition].languageSelect =
                        !list[lastPosition].languageSelect
                }
                item.languageSelect = !item.languageSelect
                lastPosition = position
                onItemClickListener?.onLanguageSelected(position)
            }
        }
    }

    override fun getItemCount(): Int = list.size


    private var onItemClickListener: OnClickListener? = null
    fun setLanguageClick(clickListener: OnClickListener) {
        this.onItemClickListener = clickListener
    }

    interface OnClickListener {
        fun onLanguageSelected(position: Int)
    }

    override fun onBindViewHolder(holder: ItemVH, position: Int) {
        holder.bindData(list[position], position)
    }
}